package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import bean.*;
import java.sql.*;
import java.util.stream.StreamSupport;


public class Bookdao {

    private ArrayList getRet(ResultSet rs) throws Exception {
        ArrayList list = new ArrayList();
        while(rs.next()) {
            Book ret = new Book();
            ret.setIsbn(rs.getString("isbn"));
            ret.setKind(rs.getString("kind"));
            ret.setLinktoimg(rs.getString("linktoimg"));
            ret.setName(rs.getString("name"));
            ret.setAuthor(rs.getString("author"));
            ret.setNum(rs.getInt("num"));
            ret.setPrice(rs.getDouble("price"));
            ret.setHead_title(rs.getString("head_title"));
            list.add(ret);
        }
        return list;
    }
    // 添加新书
    public boolean addBook(Book book) {
        DBbean db = new DBbean();
        db.getConnection();

        String sql = "insert into book (isbn, name, price, linktoimg, kind, author, head_title, num) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        ArrayList params = new ArrayList();
        params.add(book.getIsbn());
        params.add(book.getName());
        params.add(String.valueOf(book.getPrice() ));
        params.add(book.getLinktoimg());
        params.add(book.getKind());
        params.add(book.getAuthor());
        params.add(book.getHead_title());
        params.add(String.valueOf(book.getNum() ));
        try {
            db.executeUpdate(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
            db.close();
            return false;
        }
        db.close();
        return true;
    }
    // 改变书的数量，购进数，卖出书
    public boolean updateNum(Book book, int d) {
        DBbean db = new DBbean();
        db.getConnection();
        String sql = "update book set num = num" + d + " where isbn =\"" + book.getIsbn() + "\"";
        try {
            db.executeUpdate(sql, null);
        } catch (Exception e) {
            e.printStackTrace();
            db.close();
            return false;
        }
        db.close();
        return true;
    }
    // 根据isbn获取书的对象
    public Book getBook(String isbn) {
        String sql = "select * from book where isbn = ?";
        DBbean db = new DBbean();
        db.getConnection();
        ResultSet rs = null;
        Book ret = new Book();
        ArrayList list = new ArrayList();
        list.add(isbn);
        try {
            rs = db.executeQuery(sql, list);
            ret = (Book) (getRet(rs).get(0));
        } catch (Exception e) {
            e.printStackTrace();
        }
        db.close();
        return ret;
    }

    // 获取某类图书
    public ArrayList getBooks(String kind) {
        String sql = "select * from book where kind = \"" + kind + "\"";
        DBbean db = new DBbean();
        db.getConnection();
        ResultSet rs = null;
        ArrayList books = new ArrayList();
        try {
            rs = db.executeQuery(sql, null);
            books = getRet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return books;
    }
    // 获取所有书
    public ArrayList getAllBooks() {
        String sql = "select * from book";
        DBbean db = new  DBbean();
        db.getConnection();
        ResultSet rs  =  null;
        ArrayList books = new ArrayList();
        try {
            rs = db.executeQuery(sql, null);
            books = getRet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return books;
    }
    public ArrayList getSomeBooks(String name) {
        String sql = "select * from book where name like \"%" + name + "%\"";
        DBbean db = new  DBbean();
        db.getConnection();
        ResultSet rs  =  null;
        ArrayList books = new ArrayList();
        try {
            rs = db.executeQuery(sql, null);
            books = getRet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return books;
    }


}


